/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.LotteryLog;
import com.boxin.ims.modules.wechat.service.LotteryLogService;

/**
 * 抽奖日志Controller
 * @author Jakemanse
 * @version 2013-09-30
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/lotteryLog")
public class LotteryLogController extends BaseController {

	@Autowired
	private LotteryLogService lotteryLogService;
	
	@ModelAttribute
	public LotteryLog get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lotteryLogService.get(id);
		}else{
			return new LotteryLog();
		}
	}
	
	@RequiresPermissions("wechat:lotteryLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(LotteryLog lotteryLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			lotteryLog.setUser(user);
		}
        Page<LotteryLog> page = lotteryLogService.find(new Page<LotteryLog>(request, response), lotteryLog); 
        model.addAttribute("page", page);
		return "modules/wechat/lotteryLogList";
	}

	@RequiresPermissions("wechat:lotteryLog:view")
	@RequestMapping(value = "form")
	public String form(LotteryLog lotteryLog, Model model) {
		model.addAttribute("lotteryLog", lotteryLog);
		return "modules/wechat/lotteryLogForm";
	}

	@RequiresPermissions("wechat:lotteryLog:edit")
	@RequestMapping(value = "save")
	public String save(LotteryLog lotteryLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, lotteryLog)){
			return form(lotteryLog, model);
		}
		lotteryLogService.save(lotteryLog);
		addMessage(redirectAttributes, "保存抽奖日志'" + lotteryLog.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/lotteryLog/?repage";
	}
	
	@RequiresPermissions("wechat:lotteryLog:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		lotteryLogService.delete(id);
		addMessage(redirectAttributes, "删除抽奖日志成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/lotteryLog/?repage";
	}
	
	
	@RequiresPermissions("wechat:lotteryLog:view")
	@RequestMapping(value ="lotteryLog")
	public String lotteryLog(LotteryLog lotteryLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			lotteryLog.setUser(user);
		}
        Page<LotteryLog> page = lotteryLogService.findByNotMeb(new Page<LotteryLog>(request, response), lotteryLog); 
        model.addAttribute("page", page);
		return "modules/wechat/lotteryLogByNotMebList";
	}

}
